require("lua-xwrt.addon.string")
require("lua-xwrt.addon.uci")
require("lua-xwrt.html.form")
require("lua-xwrt.xwrt.translator")
require("lua-xwrt.addon.io")

--util.show_table(__FORM)
local string = string
local io = io
local os = os
local pairs, ipairs = pairs, ipairs
local table = table
local uci = uci
local util = util
local string = string
local type = type
local print = print
local tostring, tonumber = tostring, tonumber
local formClass = formClass
local htmlhmenuClass = htmlhmenuClass
local tr = tr
local __MENU = __MENU
local __FORM = __FORM
local require = require

local newMenu = htmlhmenuClass.new("submenu")
newMenu:add("Manual","urbalink.sh?option=manual")
newMenu:add("Automatica","urbalink.sh?option=auto")
if __FORM.cat == nil then __FORM.cat = "Info" end
if __MENU[__FORM.cat].len > 1 then 
	__MENU[__FORM.cat]["Urbalink"] = newMenu
else
	__MENU[__FORM.cat] = newMenu
end


module("lua-xwrt.urbalink")
local tconf = {}
local newMenu = htmlhmenuClass.new("submenu")
local ovpn = require("lua-xwrt.openvpn")

function setting(form)
	local form = form
	local forms = {}
  if form == nil then
    form = formClass.new(tr("urbalink_setting_ap#Counfiguracion Automatica del AP"))
  else
    form:Add("subtitle",tr("openvpn_client_crts#Counfiguracion Automatica del AP"))
  end
	form:Add("text","urbalink.general.apname", uci.get("urbalink","general","apname"),tr("Nombre del AP"),"string")
	form:Add("text","urbalink.general.key", uci.get("urbalink","general","key"), tr("Clave del AP"), "string")
	form:Add("select","urbalink.general.url", uci.get("urbalink","general","url"), tr("Servidor de Conf."), "string")
	form["urbalink.general.url"].options:Add("","")
	for _, v in pairs(uci.get("urbalink","server","config")) do
		local _, _, n, u = string.find(v,"(.+)|(.+)")
		form["urbalink.general.url"].options:Add(u,n)
	end
	forms[#forms+1] = form
	return forms
end

function form_manual(form)
	local forms = {}
  if form == nil then
    form = formClass.new(tr("urbalink_setting_ap#Counfiguracion Manual del AP"))
  else
    form:Add("subtitle",tr("openvpn_client_crts#Counfiguracion Manual del AP"))
  end
	form:Add("subtitle","ChilliSpot")
	form:Add("service","chillispot.service.enable","chilli",tr("chillispot_var_service#Service"),"")
	form["chillispot.service.enable"].options:Add("service","chilli")
	form["chillispot.service.enable"].options:Add("init","chilli")

	form:Add("text","chillispot.settings.radiusnasid", uci.get("chillispot","settings","radiusnasid"), tr("Nombre NAS"), "string")
	form:Add("text","chillispot.settings.radiusnasip", uci.get("chillispot","settings","radiusnasip"), tr("Chilli Nas Ip"), "string")
--	form:Add("text","chillispot.settings.radiuslocationid", uci.get("chillispot","settings","radiuslocationid"), tr("Ubicacion"), "string")
	form:Add("text","chillispot.settings.radiuslocationname", uci.get("chillispot","settings","radiuslocationname"), tr("Nombre del Lugar"), "string")
	
	form:Add("subtitle","VPN")
	form:Add("service","openvpn.service.enable","openvpn",tr("openvpn_var_service#Service"),"")
	form["openvpn.service.enable"].options:Add("service","openvpn")
	form["openvpn.service.enable"].options:Add("init","openvpn")

	form:Add("select","openvpn.urbalink.enable",uci.check_set("openvpn","urbalink","enable","1"),tr("openvpn_service#Service"),"string")
	form["openvpn.urbalink.enable"].options:Add("0","Disable")
	form["openvpn.urbalink.enable"].options:Add("1","Enable")
	form:Add("text","openvpn.urbalink.ifconfig", uci.get("openvpn","urbalink","ifconfig"), tr("VPN Ip"), "string","width:97%;")
	ovpn.form_clientCert(form,"urbalink")
	forms[#forms+1] = form
	return forms
end